package com.gxljc.bear.crawler.itaogao.cscomcn;

import com.gxljc.bear.crawler.itaogao.ItaogaoConst;
import com.gxljc.bear.crawler.proxy.ProxyUtil;
import com.gxljc.bear.crawler.util.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.*;

/**
 * 中国证券报 seed 数据爬虫。
 *
 * @author tanghaitao
 * @since 2022-9-14
 */
public class CscomcnUpdateCrawler implements Serializable {
    public static Logger LOG = Logger.getLogger(CscomcnUpdateCrawler.class);
    private Boolean proxy = false;
    private Map<String, String> proper = new HashMap<String, String>();
    CscomcnSeedCrawler crawler = null;
    public CscomcnUpdateCrawler(Boolean proxy) {

        crawler = new CscomcnSeedCrawler(proxy);
        this.proxy = proxy;
    }

    public void crawl() throws Exception {
        List<String> seeds = getSeed();
        if (CollectionUtils.isEmpty(seeds)) {
            LOG.error("page is empty");
            return;
        }
        crawlNoSpark(seeds);
    }

    public void crawlNoSpark(List<String> seeds) {
        ProxyUtil.initQueue(proxy);
        for (String url : seeds) {
            try {
                crawler.crawl(url);
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

    //构造URL seed
    public List<String> getSeed() throws Exception {
        List<Date> dateSeed = getDateSeed();
        if (CollectionUtils.isEmpty(dateSeed)) return null;
        List<String> urls = new LinkedList<String>();
        for (Date date : dateSeed) {
            String dateFormat = DateUtil.dateFormat(date, "yyyy-MM/dd");
            //http://epaper.cs.com.cn/html/2016-09/14/node_17.htm
            String url = String.format(ItaogaoConst.NEWSPAPER_SEED.CSCOMCN.getValue(), dateFormat);
            urls.add(url);
        }
        return urls;
    }

    //获取从当前到1月1号的列表
    private List<Date> getDateSeed() {
        Calendar now = Calendar.getInstance();
        List<Date> list = new LinkedList<>();
        list.add(now.getTime());
        return list;
    }

    //获取从当前到1月1号的列表
    private List<Date> getDateSeed2() {
        Calendar now = Calendar.getInstance();
        Date date = DateUtil.parseFormat("2016-10-18","yyyy-MM-dd");
        List<Date> list = new LinkedList<>();
        list.add(date);
        return list;
    }
}
